<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>组合模式典型应用之表单渲染</title>
</head>
<body>
    <script>
        class Container {
            constructor(type) {
                this.element = document.createElement(type);
                this.children = [];
            };
            add(child) {
                this.children.push(child);
                this.element.appendChild(child.element);
                return this;
            }
        }

        class Item {
            constructor(type, title) {
                this.element = document.createElement(type);
                this.element.innerHTML = title;
            }
        }

        class InputItem {
            constructor(type, name) {
                this.element = document.createElement(type);
                this.element.name = name;
            }
        }

        // 测试
        let form = new Container('form');
        let username = new Container('p');
        let passeord = new Container('p');

        let usernameLabel = new Item('label', '账号');
        let usernameInput = new InputItem('input', 'username');
        // let passwordLabel = new Item('label', '密码');

        username.add(usernameLabel).add(usernameInput);
        document.body.appendChild(username.element);
    </script>
    
</body>
</html>